<template>
  <div :class="{ hidden: hidden }" class="pagination-container">
    <el-pagination :background="background" :current-page="currentPage" :page-size="pageSize" :layout="layout" :page-sizes="pageSizes" :total="total" v-bind="$attrs" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
  </div>
</template>

<script lang="ts" setup>
import { ref, watch } from "vue";

const props = defineProps({
  total: {
    required: true,
    type: Number
  },
  page: {
    type: Number,
    default: 1
  },
  limit: {
    type: Number,
    default: 20
  },
  pageSizes: {
    type: Array,
    default() {
      return [10, 20, 30, 50];
    }
  },
  layout: {
    type: String,
    default: "total, sizes, prev, pager, next, jumper"
  },
  background: {
    type: Boolean,
    default: true
  },
  autoScroll: {
    type: Boolean,
    default: true
  },
  hidden: {
    type: Boolean,
    default: false
  }
});

const emit = defineEmits(["pagination"]);
const currentPage = ref(1);
const pageSize = ref(10);

watch(
  () => props.page,
  (val) => {
    currentPage.value = val;
  },
  {
    immediate: true
  }
);
watch(
  () => props.limit,
  (val) => {
    pageSize.value = val;
  },
  {
    immediate: true
  }
);

const handleSizeChange = (val: number) => {
  emit("pagination", { page: currentPage.value, limit: val });
};
const handleCurrentChange = (val: number) => {
  emit("pagination", { page: val, limit: pageSize.value });
};
// export default {
//   name: "Pagination",
//   props: {

//   },
//   computed: {
//     currentPage: {
//       get() {
//         return this.page;
//       },
//       set(val) {
//         this.$emit("update:page", val);
//       }
//     },
//     pageSize: {
//       get() {
//         return this.limit;
//       },
//       set(val) {
//         this.$emit("update:limit", val);
//       }
//     }
//   },
</script>

<style scoped>
.pagination-container {
  background: #fff;
  padding: 32px 16px;
}
.pagination-container.hidden {
  display: none;
}
</style>
